
open List;;

let rec from_to a b = if b < a then [] else a :: from_to (a+1) b;;

let prod xs ys =
 List.flatten (List.map (fun x -> List.map (fun y -> x,y) ys) xs);;

let lt (x,y) (x',y') =
 x < x' && y != y' && abs (x - x') != abs (y - y');;

type field = int*int
type board = field list

let rec place n qs free =
 if List.length qs = n then [qs] else
 List.flatten
   (List.map (fun f -> place n (f::qs) (List.filter (lt f) free))
      free);;

let queens n = place n [] (prod (from_to 0 (n-1)) (from_to 0 (n-1)));;

length (queens 8);;
length (queens 9);;
